# docker 安装mongo7.0.4 并挂载数据卷到宿主机
# 李广龙
# 如果启动报 ERROR: child process failed, exited with 51
# To see additional information in this output, start without the "--fork" option.
# stat: cannot statx '/proc/1/fd/1': Operation not permitted
# warning: initdb logs cannot write to '/proc/1/fd/1', so they are in '/data/db/docker-initdb.log' instead
# about to fork child process, waiting until server is ready for connections.
# 这个错误,请升级docker版本解决,升级到23.01


# mongod 和 mongosh
# 会在DATAPATH目录下创建 文件夹






DATAPATH="/data"
PORT="27017"
# 用户名 root 
PASSWORD="mongo@qwer123!"
SERVICE_NAME="mongo"

creatStart() {
  docker pull mongo:7.0.4
  mkdir -p "$DATAPATH"/mongo/db
  start

}
creatMycnf() {
# 这里没有指定配置文件,如果要自定义,请查看官方文档
# docker run --name some-mongo -v /my/custom:/etc/mongo -d mongo --config /etc/mongo/mongod.conf
  cat >./my.cnf <<EOF
EOF
}


start() {
  docker run \
    --name "$SERVICE_NAME" \
    --network host \
    -v "$DATAPATH"/mongo/db:/data/db \
  	-e MONGO_INITDB_ROOT_USERNAME=root \
	  -e MONGO_INITDB_ROOT_PASSWORD="$PASSWORD" \
    --restart=always \
    -e TZ=Asia/Shanghai \
    -d mongo:7.0.4

   echo "运行成功"
}

read -p '输入功能编号0创建挂载运行 1挂载运行: (任意键退出)' input
echo "输入编号:$input"

case $input in
0)
  creatStart
  ;;
1)
  start
  ;;
*)
  echo -e "${RED}退出sh脚本${RES}"
  exit 0
  ;;
esac
